package com.project8.main;

import java.util.ArrayList;
import com.project8.book.Chapter;
import com.project8.book.Page;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ToCAdapter extends BaseAdapter{
    private Context mContext;
    private ArrayList<Page> mThumbIds;
    private TableOfContent mainInstance;
        
    public ToCAdapter(Context c, ArrayList<Page> allThumbnails, TableOfContent main, Chapter thisChapter) 
    {
        mContext = c;
        mThumbIds = allThumbnails;
        mainInstance = main; 
    }
    
    public int getCount() {
        return (int)mThumbIds.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) 
    {    	
       LinearLayout pageView;
       TextView label = new TextView(mContext);
       WebView foreground = new WebView(mContext);
       ImageView bookmarkIcon = new ImageView(mContext);
       LinearLayout pageInfo = new LinearLayout(mContext);
       
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            pageView = new LinearLayout(mContext);
            pageView.setLayoutParams(new GridView.LayoutParams(100, 135));

    		foreground.getSettings().setUseWideViewPort(true);
    		foreground.getSettings().setLoadWithOverviewMode(true);
    		pageView.setPadding(12, 12, 12, 12);
        } else {
            pageView = (LinearLayout)convertView;
        }
        
        pageView.setOrientation(LinearLayout.VERTICAL);
        label.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        foreground.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
        
        label.setText("Page " + mThumbIds.get(position).getPageNum());
        
        if(!mThumbIds.get(position).getPageName().equals("Quiz.html"))
        	foreground.loadUrl(mThumbIds.get(position).getPageContent());
        bookmarkIcon.setImageResource(R.drawable.icon_bookmarked_small);
        
        pageInfo.setOrientation(LinearLayout.HORIZONTAL);
        pageView.setBackgroundResource(R.drawable.page);
        
        label.setGravity(Gravity.CENTER_HORIZONTAL);
        label.setTextColor(Color.parseColor("#FF000000"));
        
        //pageView.addView(label);
        //pageView.addView(bookmarkIcon);
        
        if(mThumbIds.get(position).isBookmarked())
        {
        	bookmarkIcon.setVisibility(View.VISIBLE);
        	
        	System.out.println("Page #" + position + " is bookmarked.");
        }
        else
        {
        	bookmarkIcon.setVisibility(View.GONE);
        	System.out.println("Page #" + position + " is NOT bookmarked.");
        }
        
        pageInfo.addView(bookmarkIcon);
        pageInfo.addView(label);
        
        foreground.setHorizontalScrollBarEnabled(false);
        foreground.setVerticalScrollBarEnabled(false); 
                
        pageView.addView(pageInfo);
        pageView.addView(foreground);

        final int pos2 = position;
                	    
        OnTouchListener listener = new OnTouchListener()
	    {
	    	WebView popOut = (WebView)mainInstance.findViewById(R.id.popOutPage);	//Displays pop-up thumbnail
	    	
	    	long lastTouchTime = -1;
	    	View lastTouched;
	    		    	
			public boolean onTouch(View arg0, MotionEvent arg1) 
			{
	    		popOut.getSettings().setUseWideViewPort(true);		   //Double-Tap .. Zoom in like an image, don't just change text size
	    		popOut.getSettings().setLoadWithOverviewMode(true);   //Fit content to screen
							
				if(arg1.getAction() == MotionEvent.ACTION_DOWN)
				{
				    long thisTime = System.currentTimeMillis();
				    
				    if ((thisTime - lastTouchTime < 800) && (arg0.equals(lastTouched)))			//Double Tap
				    {
				    	mainInstance.returnResult(mThumbIds.get(pos2)); // Go to appropriate page
				    }
				    else 
				    {
				         // too slow
				         lastTouchTime = thisTime;
				         popOut.loadUrl(mThumbIds.get(pos2).getPageContent());
						 popOut.setVisibility(View.VISIBLE);						 
				    }
				    
				    lastTouched = arg0;
										
					return true;
				}

				if(arg1.getAction() == MotionEvent.ACTION_UP)
				{
					popOut.setVisibility(View.GONE);
					popOut.loadUrl("");
										
					return true;
				}
				if(arg1.getAction() == MotionEvent.ACTION_CANCEL)
				{
					popOut.setVisibility(View.GONE);
										
					return true;
				}
				
				return true;	
			}	
	    };
        
	    
	    pageView.setOnTouchListener(listener);
	    label.setOnTouchListener(listener);
	    foreground.setOnTouchListener(listener);

        return pageView;
    }
}

